set(APP_LIBS libc)

# Do we need to link with the local compiler runtime?
if (NOT ${TOOLCHAIN_RT})
  list(APPEND APP_LIBS rt)
endif(NOT ${TOOLCHAIN_RT})

#
# C Library Sources
#
add_subdirectory(libc/)


#
# Applications
#
include_directories(SYSTEM
		    ${PROJECT_SOURCE_DIR}/apps/libc
		    ${PROJECT_SOURCE_DIR}/include)
set(APPS_C_FLAGS "-std=c99 -nostdlib -fno-builtin ${TOOLCHAIN_C_FLAGS} ${TARGET_C_FLAGS}")
set(APPS_LINK_FLAGS "-T '${CMAKE_SOURCE_DIR}/apps/apps.ld' ${TOOLCHAIN_LINK_FLAGS}")

#
# ls
#
set(LS_SRC ls.c)

add_executable(ls ${LS_SRC})
target_link_libraries(ls ${APP_LIBS})
set_target_properties(ls PROPERTIES
  LINK_FLAGS ${APPS_LINK_FLAGS})
set_source_files_properties(${LS_SRC} PROPERTIES
  COMPILE_FLAGS "${APPS_C_FLAGS}")

#
# hello
#
set(HELLO_SRC hello.c)

add_executable(hello ${HELLO_SRC})
target_link_libraries(hello ${APP_LIBS})
set_target_properties(hello PROPERTIES
  LINK_FLAGS ${APPS_LINK_FLAGS})
set_source_files_properties(${HELLO_SRC} PROPERTIES
  COMPILE_FLAGS "${APPS_C_FLAGS}")


#
# video
#
set(VIDEO_SRC video.c gfx.c)

add_executable(video ${VIDEO_SRC})
target_link_libraries(video ${APP_LIBS})
set_target_properties(video PROPERTIES
  LINK_FLAGS ${APPS_LINK_FLAGS})
set_source_files_properties(${VIDEO_SRC} PROPERTIES
  COMPILE_FLAGS "${APPS_C_FLAGS}")

#
# init
#
set(INIT_SRC init.c)

add_executable(init ${INIT_SRC})
target_link_libraries(init ${APP_LIBS})
set_target_properties(init PROPERTIES
  LINK_FLAGS ${APPS_LINK_FLAGS})
set_source_files_properties(${INIT_SRC} PROPERTIES
  COMPILE_FLAGS "${APPS_C_FLAGS}")

#
# sh
#
set(SH_SRC sh.c)

add_executable(sh ${SH_SRC})
target_link_libraries(sh ${APP_LIBS})
set_target_properties(sh PROPERTIES
  LINK_FLAGS ${APPS_LINK_FLAGS})
set_source_files_properties(${SH_SRC} PROPERTIES
  COMPILE_FLAGS "${APPS_C_FLAGS}")

#
# cat
#
set(CAT_SRC cat.c)

add_executable(cat ${CAT_SRC})
target_link_libraries(cat ${APP_LIBS})
set_target_properties(cat PROPERTIES
  LINK_FLAGS ${APPS_LINK_FLAGS})
set_source_files_properties(${CAT_SRC} PROPERTIES
  COMPILE_FLAGS "${APPS_C_FLAGS}")

#
# ps
#
set(PS_SRC ps.c)

add_executable(ps ${PS_SRC})
target_link_libraries(ps ${APP_LIBS})
set_target_properties(ps PROPERTIES
  LINK_FLAGS ${APPS_LINK_FLAGS})
set_source_files_properties(${PS_SRC} PROPERTIES
  COMPILE_FLAGS "${APPS_C_FLAGS}")

#
# video-ps
#
set(VIDEO_PS_SRC video-ps.c gfx.c)

add_executable(video-ps ${VIDEO_PS_SRC})
target_link_libraries(video-ps ${APP_LIBS})
set_target_properties(video-ps PROPERTIES
  LINK_FLAGS ${APPS_LINK_FLAGS})
set_source_files_properties(${VIDEO_PS_SRC} PROPERTIES
  COMPILE_FLAGS "${APPS_C_FLAGS}")

#
# stress
#
set(STRESS_SRC stress.c)

add_executable(stress ${STRESS_SRC})
target_link_libraries(stress ${APP_LIBS})
set_target_properties(stress PROPERTIES
  LINK_FLAGS ${APPS_LINK_FLAGS})
set_source_files_properties(${STRESS_SRC} PROPERTIES
  COMPILE_FLAGS "${APPS_C_FLAGS}")

#
# mkdir
#
set(MKDIR_SRC mkdir.c)

add_executable(mkdir ${MKDIR_SRC})
target_link_libraries(mkdir libc)
set_target_properties(mkdir PROPERTIES
  LINK_FLAGS ${APPS_LINK_FLAGS})
set_source_files_properties(${MKDIR_SRC} PROPERTIES
  COMPILE_FLAGS "${APPS_C_FLAGS}")

#
# rmdir
#
set(RMDIR_SRC rmdir.c)

add_executable(rmdir ${RMDIR_SRC})
target_link_libraries(rmdir libc)
set_target_properties(rmdir PROPERTIES
  LINK_FLAGS ${APPS_LINK_FLAGS})
set_source_files_properties(${RMDIR_SRC} PROPERTIES
  COMPILE_FLAGS "${APPS_C_FLAGS}")

#
# overwrite
#
set(OVERWRITE_SRC overwrite.c)

add_executable(overwrite ${OVERWRITE_SRC})
target_link_libraries(overwrite libc)
set_target_properties(overwrite PROPERTIES
  LINK_FLAGS ${APPS_LINK_FLAGS})
set_source_files_properties(${OVERWRITE_SRC} PROPERTIES
  COMPILE_FLAGS "${APPS_C_FLAGS}")
